<?php

class Area extends AppModel
{

    var $name = 'Area';
    var $hasMany = 'Articles';
    var $hasAndBelongsToMany = array(
        'Congress' => array(
            'className' => 'Congress',
            'joinTable' => 'areas_congresses',
            'foreignKey' => 'area_id',
            'associationForeignKey' => 'congress_id',
        ),
        'User' => array(
            'className' => 'User',
            'joinTable' => 'areas_users',
            'foreignKey' => 'area_id',
            'associationForeignKey' => 'user_id',
        )
    );

    // retorna um array de áreas que pertencem ao congresso e que possuem avaliadores
    function findAreas($congressId)
    {
        return $this->query("SELECT `id` , `name`
                                     FROM `areas`
                                     WHERE `id`
                                     IN (
                                        SELECT `areas_congresses`.`area_id`
                                        FROM `areas_congresses`
                                        WHERE `congress_id` = '$congressId'
                                     )
                                     AND `id`
                                     IN (
                                        SELECT `area_id`
                                        FROM `areas_users`
                                        WHERE `area_id`
                                        IN (
                                            SELECT `areas_congresses`.`area_id`
                                            FROM `areas_congresses`
                                            WHERE `congress_id` = '$congressId'
                                        )
                                     )");
    }

}

?>
